package cn.edu.xmu.javaee.productdemomybatis.dao;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.util.JacksonUtil;
import cn.edu.xmu.javaee.productdemomybatis.dao.bo.Product;
import cn.edu.xmu.javaee.productdemomybatis.mapper.manual.ProductMapper;
import cn.edu.xmu.javaee.productdemomybatis.mapper.manual.po.ProductAllPo;
import cn.edu.xmu.javaee.productdemomybatis.util.CloneFactory;
import cn.edu.xmu.javaee.productdemomybatis.util.RequestVariables;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.edu.xmu.javaee.core.model.Constants.PLATFORM;


/**
 * 定义在BeanConfig里
 */
@Slf4j
@RequiredArgsConstructor
public class MybatisProductDao implements FindProduct{

    private final ProductMapper productMapper;
    /**
     * 用手写的方式实现关联的Product
     * @param shopId 商铺id
     * @param productId 产品id
     * @return
     * @throws BusinessException
     */
    @Override
    public Product findById(Integer shopId, Integer productId) throws BusinessException {
        Product product = null;
        log.debug("findById: shopId = {}, productId = {}", shopId, productId);
        try {
            ProductAllPo productPo = this.productMapper.getProductById(productId);
            log.debug("findById: productPo = {}", productPo);

            if (Objects.isNull(productPo)) {
                String[] objects = new String[] {"${product}", productId.toString()};
                throw new BusinessException(ReturnNo.RESOURCE_ID_NOTEXIST, JacksonUtil.toJson(objects));
            }else{
                if (!Objects.equals(shopId, productPo.getShopId()) && !PLATFORM.equals(shopId)){
                    String[] objects = new String[] {"${product}", productId.toString(), shopId.toString()};
                    throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, JacksonUtil.toJson(objects));
                }

            }

            product = CloneFactory.copy(new Product(), productPo);
            log.debug("findById: product = {}", product);
        } catch (DataAccessException e) {
            log.error("findById: error = {}", e);
            throw new BusinessException(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
        return product;
    }

    /**
     * 用手写的方式实现关联的Product
     * @param shopId 商铺id 为PLATFROM则在全系统寻找，否则在商铺内寻找
     * @param name 名称
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Product> retrieveByName(Integer shopId, String name) throws BusinessException {
        List<Product> productList;
        try {
            List<ProductAllPo> productPoList;
            if (PLATFORM.equals(shopId)) {
                productPoList = this.productMapper.getProductByName(name);
            }else{
                productPoList = this.productMapper.getProductByShopIdAndName(shopId, name);
            }

            log.debug("retrieveByName: productPoList = {}", productPoList);
            productList = productPoList.stream().map(o -> CloneFactory.copy(new Product(), o)).collect(Collectors.toList());
            log.debug("retrieveByName: productList = {}", productList);
        } catch (DataAccessException e) {
            log.error(e.getMessage());
            throw new BusinessException(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
        return productList;
    }

}
